<template>
  <!-- 添加或修改考试题库对话框 -->
  <el-dialog :visible.sync="visible" :title="modalObj.title" :width="modalObj.width" :modal='modalObj.modal'
             center :close-on-click-modal="true"
             :close-on-press-escape="true"
             @closed="$emit('update:visible', false)"
              append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="试卷ID" prop="paperId">
        <el-input v-model="form.paperId" placeholder="请输入试卷ID"/>
      </el-form-item>
      <el-form-item label="题库" prop="repositoryId">
        <el-input v-model="form.repositoryId" placeholder="请输入题库"/>
      </el-form-item>
      <el-form-item label="单选题数量" prop="radioCount">
        <el-input v-model="form.radioCount" placeholder="请输入单选题数量"/>
      </el-form-item>
      <el-form-item label="单选题分数" prop="radioScore">
        <el-input v-model="form.radioScore" placeholder="请输入单选题分数"/>
      </el-form-item>
      <el-form-item label="多选题数量" prop="multiCount">
        <el-input v-model="form.multiCount" placeholder="请输入多选题数量"/>
      </el-form-item>
      <el-form-item label="多选题分数" prop="multiScore">
        <el-input v-model="form.multiScore" placeholder="请输入多选题分数"/>
      </el-form-item>
      <el-form-item label="填空题数量" prop="fillCount">
        <el-input v-model="form.fillCount" placeholder="请输入填空题数量"/>
      </el-form-item>
      <el-form-item label="填空题分数" prop="fillScore">
        <el-input v-model="form.fillScore" placeholder="请输入填空题分数"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getPaperRepository, addPaperRepository, updatePaperRepository } from '@/api/exam/paperRepository'

  export default {
    name: 'PaperRepositoryEdit',

    props: {
      visible: {
        type: Boolean,
        default: false
      },
      modalObj: {
        type: Object,
        default: false
      }
    },
    data() {
      return {
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          paperId: [
            { required: true, message: '试卷ID不能为空', trigger: 'blur' }
          ],
          repositoryId: [
            { required: true, message: '题库不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.reset()
      this.getData()
    },
    methods: {
      /** 查询考试题库列表 */
      getData() {
        if (this.modalObj.id) {
          getPaperRepository(this.modalObj.id).then(response => {
            this.form = response.data
          })
        }
      },
      // 取消按钮
      cancel() {
        this.$emit('update:visible', false)
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          paperId: null,
          repositoryId: null,
          radioCount: null,
          radioScore: null,
          multiCount: null,
          multiScore: null,
          fillCount: null,
          fillScore: null
        }
        this.resetForm('form')
      },

      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePaperRepository(this.form).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.$emit('update:visible', false)
                this.$emit('handleClose')
              })
            } else {
              addPaperRepository(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.$emit('update:visible', false)
                this.$emit('handleClose')
              })
            }
          }
        })
      }

    }
  }
</script>
